using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Xml;
using Chaotic.DataBase.DataAccess;
using Chaotic.DataBase.DataAccess.Entity;
using MongoDB;

namespace SkynetMongoDBAccess;

public class MongoAccess : IDataAccess, IDataStructure
{
	private readonly MongoKeyValueDB _mongo;

	private bool _isClose;

	private readonly ConvertSql _convert = new ConvertSql();

	public DatabaseType DatabaseType => DatabaseType.MongoDB;

	public IDbConnection DbConnection => null;

	public bool IsClosed => _isClose;

	public string Owner { get; set; }

	public MongoAccess(string connectionString)
	{
		_mongo = new MongoKeyValueDB(connectionString);
	}

	public IDbTransaction? BeginTransaction()
	{
		return null;
	}

	public void Open()
	{
		_mongo.Open();
	}

	public void Close()
	{
		_mongo.Dispose();
		_isClose = true;
	}

	public int ExecuteNonQuery(CommandType commandType, string? commandText)
	{
		return ExecuteNonQuery(commandText);
	}

	public int ExecuteNonQuery(string? commandText)
	{
		if (commandText.StartsWith("insert", StringComparison.CurrentCultureIgnoreCase))
		{
			_convert.ConvertInsert(commandText, _mongo.GetDataBase);
			return 0;
		}
		if (commandText.StartsWith("update", StringComparison.CurrentCultureIgnoreCase))
		{
			_convert.ConvertUpdate(commandText, _mongo.GetDataBase);
			return 0;
		}
		if (commandText.StartsWith("delete", StringComparison.CurrentCultureIgnoreCase))
		{
			_convert.ConvertDelete(commandText, _mongo.GetDataBase);
			return 0;
		}
		throw new Exception("无效命令");
	}

	public int ExecuteNonQuery(string? commandText, QueryParameterCollection commandParameters)
	{
		return ExecuteNonQuery(commandText);
	}

	public int ExecuteNonQuery(DataRow row, SqlStruct sqlStruct)
	{
		throw new NotImplementedException();
	}

	public int ExecuteNonQuery(CommandType commandType, string? commandText, QueryParameterCollection commandParameters)
	{
		return ExecuteNonQuery(commandText);
	}

	public int ExecuteNonQuery(CommandType commandType, string? commandText, QueryParameterCollection commandParameters, DataRow row, SqlStruct sqlStruct)
	{
		return ExecuteNonQuery(commandText);
	}

	public DataSet? ExecuteDataset(CommandType commandType, string? commandText)
	{
		return ExecuteDataset(commandText);
	}

	public DataSet? ExecuteDataset(string? commandText)
	{
		return _convert.ConvertSelect(commandText, _mongo.GetDataBase) as DataSet;
	}

	public DataSet? ExecuteDataset(CommandType commandType, string? commandText, QueryParameterCollection commandParameters)
	{
		return ExecuteDataset(commandText);
	}

	public DataSet? ExecuteDataset(string? commandText, QueryParameterCollection commandParameters)
	{
		return ExecuteDataset(commandText);
	}

	public DataSet? ExecuteDataset(CommandType commandType, string? commandText, string tableName)
	{
		return ExecuteDataset(commandText);
	}

	public DataSet? ExecuteDataset(string? commandText, string tableName)
	{
		return ExecuteDataset(commandText);
	}

	public DataSet? ExecuteDataset(CommandType commandType, string? commandText, QueryParameterCollection commandParameters, string tableName)
	{
		return ExecuteDataset(commandText);
	}

	public DataSet? ExecuteDataset(string? commandText, QueryParameterCollection commandParameters, string tableName)
	{
		return ExecuteDataset(commandText);
	}

	public DataSet? ExecuteDataset(CommandType commandType, string? commandText, DataSet? ds)
	{
		ds = ExecuteDataset(commandText);
		return ds;
	}

	public DataSet? ExecuteDataset(string? commandText, DataSet? ds)
	{
		ds = ExecuteDataset(commandText);
		return ds;
	}

	public DataSet? ExecuteDataset(CommandType commandType, string? commandText, QueryParameterCollection commandParameters, DataSet? ds)
	{
		ds = ExecuteDataset(commandText);
		return ds;
	}

	public DataSet? ExecuteDataset(string? commandText, QueryParameterCollection commandParameters, DataSet? ds)
	{
		ds = ExecuteDataset(commandText);
		return ds;
	}

	public DataSet? ExecuteDataset(CommandType commandType, string? commandText, DataSet? ds, string tableName)
	{
		ds = ExecuteDataset(commandText);
		return ds;
	}

	public DataSet? ExecuteDataset(string? commandText, DataSet? ds, string tableName)
	{
		ds = ExecuteDataset(commandText);
		return ds;
	}

	public DataSet? ExecuteDataset(CommandType commandType, string? commandText, QueryParameterCollection commandParameters, DataSet? ds, string tableName)
	{
		ds = ExecuteDataset(commandText);
		return ds;
	}

	public DataSet? ExecuteDataset(string? commandText, QueryParameterCollection commandParameters, DataSet? ds, string tableName)
	{
		ds = ExecuteDataset(commandText);
		return ds;
	}

	public DataSet? ExecuteDataset(string? commandText, QueryParameterCollection commandParameters, DataSet? ds, int startRecord, int maxRecords, string tableName)
	{
		ds = ExecuteDataset(commandText);
		return ds;
	}

	public IDataReader ExecuteReader(CommandType commandType, string? commandText)
	{
		throw new NotImplementedException();
	}

	public IDataReader ExecuteReader(string? commandText)
	{
		throw new NotImplementedException();
	}

	public IDataReader ExecuteReader(CommandType commandType, string? commandText, QueryParameterCollection commandParameters)
	{
		throw new NotImplementedException();
	}

	public IDataReader ExecuteReader(string? commandText, QueryParameterCollection commandParameters)
	{
		throw new NotImplementedException();
	}

	public object? ExecuteScalar(CommandType commandType, string? commandText)
	{
		return ExecuteScalar(commandText);
	}

	public object? ExecuteScalar(string? commandText)
	{
		return _convert.ConvertSelect(commandText, _mongo.GetDataBase);
	}

	public object? ExecuteScalar(CommandType commandType, string? commandText, QueryParameterCollection commandParameters)
	{
		return ExecuteScalar(commandText);
	}

	public object? ExecuteScalar(string? commandText, QueryParameterCollection commandParameters)
	{
		throw new NotImplementedException();
	}

	public XmlReader ExecuteXmlReader(CommandType commandType, string? commandText)
	{
		throw new NotImplementedException();
	}

	public XmlReader ExecuteXmlReader(string? commandText)
	{
		throw new NotImplementedException();
	}

	public XmlReader ExecuteXmlReader(CommandType commandType, string? commandText, QueryParameterCollection commandParameters)
	{
		throw new NotImplementedException();
	}

	public XmlReader ExecuteXmlReader(string? commandText, QueryParameterCollection commandParameters)
	{
		throw new NotImplementedException();
	}

	public void FillingTables(List<TableInfo>? info)
	{
		_mongo.GetCollecton(_mongo.DBName).ForEach(delegate(string o)
		{
			info.Add(new TableInfo
			{
				TableName = o
			});
		});
	}

	public void FillingColumns(List<ColumnInfo> info)
	{
		_mongo.GetCollecton(_mongo.DBName).ForEach(delegate(string o)
		{
			try
			{
				Document document = _mongo.GetDataBase.GetCollection(o).FindOne(null);
				if (document != null)
				{
					ICollection<string> keys = document.Keys;
					if (keys.Count > 0)
					{
						info.AddRange(keys.Select((string column) => new ColumnInfo
						{
							TableName = o,
							ColumnName = column
						}));
					}
				}
			}
			catch
			{
			}
		});
	}

	public void FillingIndexs(List<IndexInfo>? info)
	{
	}

	public void FillingRelations(List<RelationshipInfo>? info)
	{
	}

	public void FillingViews(List<ViewInfo> info)
	{
	}

	public void FillingProcs(List<ProcInfo> info)
	{
	}

	public void FillingProcParams(List<ProcParamInfo> info)
	{
	}

	public void FillingTypeInfos(List<TypeNameInfos> info)
	{
	}
}
